package com.yonyou.brigade.third_parties.hcholiday.controller;

import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.model.IChannelConfig;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IUserCenterService;
import com.yonyou.brigade.order_center.model.ThirdResult;
import com.yonyou.brigade.third_parties.hcholiday.model.HcHolidayConfig;
import com.yonyou.brigade.third_parties.hcholiday.service.IHcHolidayUserService;
import com.yonyou.brigade.third_parties.hcholiday.util.HcHolidayUserUtil;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author kangfw5
 * @since 2019-07-25
 */
@RestController
@RequestMapping("/hcholiday")
public class HcHolidayController {
    @Autowired
    private IHcHolidayUserService hcHolidayUserService;
    @Autowired
    private IBriChannelService briChannelService;
    @Autowired
    private IUserCenterService userCenterService;

    @RequestMapping(path = "/login", method = RequestMethod.GET)
    public void login(String tenant, String org, String phone, String pageType, HttpServletResponse response) throws IOException {
        String loginUrl = hcHolidayUserService.getLoginUrl(tenant, org, phone, pageType, "H5", null);
        response.sendRedirect(loginUrl);
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public Object addUser(String tenant, String org, String phone) {
        ThirdResult result = new ThirdResult();
        BriChannel briChannel = briChannelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.HCHOLIDAY.getName());
        if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
            IChannelConfig channelConfig = briChannelService.getChannelConfig(tenant, ChannelEnum.HCHOLIDAY.getName(), org, new HcHolidayConfig());
            UcUserDto ucUserDto = userCenterService.getCompleteUcUserDtoByPhone(phone, tenant);
            HcHolidayConfig config = (HcHolidayConfig) channelConfig;
            result = hcHolidayUserService.addUser(channelConfig, HcHolidayUserUtil.ucUser2Hc(tenant, config.getCuicode(), ucUserDto));
        }
        return result;
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public Object updateUser(String tenant, String org, String phone) {
        ThirdResult thirdResult = new ThirdResult();
        BriChannel briChannel = briChannelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.HCHOLIDAY.getName());
        if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
            IChannelConfig channelConfig = briChannelService.getChannelConfig(tenant, ChannelEnum.HCHOLIDAY.getName(), org, new HcHolidayConfig());
            UcUserDto ucUserDto = userCenterService.getCompleteUcUserDtoByPhone(phone, tenant);
            HcHolidayConfig config = (HcHolidayConfig) channelConfig;
            thirdResult = hcHolidayUserService.updateUser(channelConfig, HcHolidayUserUtil.ucUser2Hc(tenant, config.getCuicode(), ucUserDto));
        }
        return thirdResult;
    }

    @RequestMapping(value = "/syncUser", method = RequestMethod.POST)
    public Object syncUser(String tenant, String org) {
        ThirdResult thirdResult = new ThirdResult();
        BriChannel briChannel = briChannelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.HCHOLIDAY.getName());
        if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
            IChannelConfig channelConfig = briChannelService.getChannelConfig(tenant, ChannelEnum.HCHOLIDAY.getName(), org, new HcHolidayConfig());
            List<UcUserDto> ucUserDtoList = userCenterService.getUcUsersByTenantId(tenant);
            HcHolidayConfig config = (HcHolidayConfig) channelConfig;
            thirdResult = hcHolidayUserService.syncUsers(channelConfig, HcHolidayUserUtil.ucUser2Hc(tenant, config.getCuicode(), ucUserDtoList));
        }
        return thirdResult;
    }
}
